use $pathdata/Beliefs/Beliefs_cleaned.dta, clear

preserve

keep if treatment==0 

gen se_cu=.

gen bayes_round = round(bayesian_posterior, 5)

levelsof bayes_round

foreach i in `r(levels)'{
	cap qreg2 cognitive_uncertainty if bayes_round==`i', cl(id)
	cap replace se_cu=_se[_cons] if bayes_round==`i'
}

collapse (p50) cognitive_uncertainty  se_cu, by(bayes_round)

gen upper=cognitive_uncertainty+se_cu
gen lower=cognitive_uncertainty-se_cu

tw 	(scatter cognitive_uncertainty bayes_round, msize(medsmall)  ms(o) mcolor(black)) ///
	(rcap upper lower bayes_round, msiz(large) lcolor(black)),	///
	ytitle("Cognitive Uncertainty") ///
    xtitle("Bayesian posterior") xscale( lcolor(none)) ysc(lcolor(none) r(0 0.9)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	yline(0, lcolor(gs10) lwidth(thin)) /// 
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) legend(off)


restore

** Compound beliefs figure

preserve

keep if rocl_set==1

egen bucket=group(rocl round_bayesian_posterior)
gen se_belief=.

forvalues i=1/16{
	qui qreg2 belief if bucket==`i', cluster(id)
	qui replace se_belief=_se[_cons] if bucket==`i'
}


collapse (p50) belief se_belief (count) no=belief, by(round_bayesian_posterior rocl)

gen upper=belief+se
gen lower=belief-se

twoway 	(scatter belief round_bayesian_posterior if rocl==0, msize(medsmall) mcolor(midblue%90)) ///
		(scatter belief round_bayesian_posterior if rocl==1, ms(X) msize(medium) mcolor(red%70)) ///
		(rcap upper lower round_bayesian_posterior if rocl==0, msiz(large) lcolor(midblue%90))	///
		(rcap upper lower round_bayesian_posterior if rocl==1, msiz(large) lcolor(red%70))	///
		(line round_bayesian_posterior round_bayesian_posterior, lpattern(dash) lcolor(gs10)), ///
		ytitle("Stated posterior") ///
		yline(0, lcolor(gs10) lwidth(thin)) ///
		xscale( lcolor(none)) ysc(lcolor(none)) xtitle("Bayesian posterior") title("Compound belief updating", color(black)) ///
		graphregion(color(white)) 	ylabel(, tlc(none) glcolor(gs15) glwidth(thin)) ///
		legend(order(1 2 3 5) label(1 "Baseline problem") label(2 "Compound diagnosticity") label(3 "{c 177}1 std. error of median") label(5 "Bayesian prediction") r(2))
		
restore
